package moviedatabase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.regex.Pattern;
import org.htmlparser.parserapplications.StringExtractor;
import org.htmlparser.util.ParserException;

public class TitleLinksExtractor {

	/**
	 * @param args
	 * @throws ParserException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, ParserException {
		File diretorio = new File("./data/linkPages/");
		String[] files = diretorio.list();
		
		PrintWriter pw = new PrintWriter("./data/TitleUrls.txt");
		
		for (int i = 0; i < files.length; i++) {
			int numTitles = extraiIndice(files[i], pw);
			System.out.println((i + 1) + ". " + files[i] + " - " + numTitles + " links encontrados");
		}
		
		pw.close();
	}
	
	public static int extraiIndice(String arquivo, PrintWriter pw) throws FileNotFoundException, ParserException {
		StringExtractor parser = new StringExtractor("./data/linkPages/" + arquivo);
		String texto = parser.extractStrings(true);
		String[] linhas = texto.split("\n");
		
		String token;
		int numTitles = 0;
		for (int i = 0; i < linhas.length; i++) {
			Scanner scanner = new Scanner(linhas[i]);			
			while ((token = scanner.findInLine(Pattern.compile("/title/tt\\d\\d\\d\\d\\d\\d\\d"))) != null) {
				pw.println("http://www.imdb.com" + token);
				numTitles++;
			}
		}
		
		return numTitles;
	}
}